//
//  FlatListNearCell.m
//  atrapalo
//
//  Created by Delfin Pereiro on 18/07/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "FlatListNearCell.h"
#import "Defines.h"
#import "ListFlat.h"
#import "FinAppsImageView.h"

@interface FlatListNearCell()

- (void)loadRestaurantPhotoFromRestaurant:(ListFlat*)flat;
- (void)setDistanceLabelText:(ListFlat*)listFlat;
@end

@implementation FlatListNearCell

#define DISTANCE_MAX_Y 290


@synthesize cityLabel = _cityLabel;
@synthesize streetLabel = _streetLabel;
@synthesize priceLabel = _priceLabel;
@synthesize infoLabel = _infoLabel;
@synthesize distanceLabel = _distanceLabel;
@synthesize dropPriceImage = _dropPriceImage;
@synthesize flatImage = _flatImage;

- (void)dealloc { 
    [_cityLabel release];
    [_streetLabel release];
    [_priceLabel release];
    [_infoLabel release];
    [_distanceLabel release];
    [_dropPriceImage release];
    [_flatImage release];
    [super dealloc];
}

#pragma mark - Public methods
- (void)setOddColor {
    UIView *v = [[UIView alloc] initWithFrame:self.frame];
    v.backgroundColor = [UIColor greenColor];
    self.backgroundView = v;
    [v release];
}

- (void)setEvenColor {
    UIView *v = [[UIView alloc] initWithFrame:self.frame];
    v.backgroundColor = [UIColor whiteColor];
    self.backgroundView = v;
    [v release];
}

- (void)setCellComponentsWithFlat:(ListFlat*)flat {
    [_cityLabel         setText:[flat city]];
    [_streetLabel       setText:[flat street]];
    [_infoLabel         setText:[NSString stringWithFormat:@"%@, %dm, %d%@",[flat flatType], [flat dimensions],[flat numRooms],NSLocalizedString(@"_habAcronim", @"")]];
   
    [self setDistanceLabelText:flat];
    
    [_priceLabel setText:[NSString stringWithFormat:@"%.2f€",[flat price]]];
    if ([flat lowerPrice]) {
        [_priceLabel setTextColor:[UIColor colorWithRed:236/255.0 green:28/255.0 blue:36/255.0 alpha:1]];
        [_dropPriceImage setHidden:NO];
    }
    else {
        [_priceLabel setTextColor:[UIColor blackColor]];
        [_dropPriceImage setHidden:YES];
    }
    
    [self loadRestaurantPhotoFromRestaurant:flat];

}

#pragma mark - Private methods

//------------------------------------------------------------------------------
- (void) loadRestaurantPhotoFromRestaurant:(ListFlat*)flat {   
    // Remove the previously loaded MVYImageView from the cell
    UIView *oldLabel = [self viewWithTag:6969];
    [oldLabel removeFromSuperview];
    
    CGRect rect = CGRectMake(.0f, .0f, _flatImage.frame.size.width, _flatImage.frame.size.height);   
    FinAppsImageView *remoteImage = [[FinAppsImageView alloc] initWithFrame: rect ];
    [remoteImage setTag:6969];
    [remoteImage setContentMode:UIViewContentModeScaleAspectFit];
    //DLog(@"%@",[(File*)restaurant imageUrl]);
    [remoteImage loadImageFromURL:[NSURL URLWithString:[flat imageURL]] loadingImage:[UIImage imageNamed:@"comun_img_listado_cargando.png"]];    
    [_flatImage addSubview:remoteImage];
    [remoteImage release];
}

//------------------------------------------------------------------------------
- (void)setDistanceLabelText:(ListFlat*)listFlat {
    if (!listFlat.distance || listFlat.distance == NSIntegerMax || listFlat.distance == 0) {
        [_distanceLabel setText:@""];
    } else {
        [_distanceLabel setAlpha:1];
    }
    
    if (listFlat.distance == 0) {
        [_distanceLabel setText:@""];
    } else if (listFlat.distance > 1000) {
        [_distanceLabel setText:[NSString stringWithFormat:@"(%.1f km)", listFlat.distance/1000]];
    } else {
        [_distanceLabel setText:[NSString stringWithFormat:@"(%.0f m)", listFlat.distance]];
    } 
}
@end
